package org.example.service.Impl;

import lombok.AllArgsConstructor;
import org.example.dao.Comment;
import org.example.dao.Song;
import org.example.dao.User;
import org.example.enums.responseEnum;
import org.example.mapper.CommentMapper;
import org.example.mapper.UserMapper;
import org.example.service.CommentService;
import org.example.service.SongService;
import org.example.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@AllArgsConstructor
@Service
public class CommentServiceImpl implements CommentService {
    private final CommentMapper commentMapper;
    private final UserService userService;
    private final SongService songService;

    //添加评论
    @Override
    public responseEnum addComment(Comment comment) {
        Integer i = commentMapper.addComment(comment);
        if (i > 0) {
            //添加评论成功，更新歌曲评论数量
            Song song = new Song();
            song.setId(comment.getSid());
            song.setCommentCount(1);
            songService.updateSongCommentCount(song);
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }

    //获取歌曲评论
    @Override
    public List<Comment> getComment(Comment comment) {
        // 获取评论列表
        List<Comment> commentList = commentMapper.getCommentList(comment);
        // 遍历评论列表，提取每个评论的用户ID并添加到用户ID列表中
        commentList.forEach(comment1 -> {
            User u = new User();
            u.setId(comment1.getUid());
            User userById = userService.getUserById(u);
            comment1.setUser(userById);
        });
        // 返回包含用户信息的评论列表
        return commentList;
    }

    @Override
    public responseEnum deleteComment(Comment comment) {
        Integer i = commentMapper.deleteComment(comment);
        if (i > 0) {
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }


}
